<script>

    function addListenerSetRangeButton() {
        // 根据价格区间筛选商品
        $("#setRangeBtn").click(() => {
            // 获取价格区间
            let priceMin = parseInt($("#priceMinInput").val());
            let priceMax = parseInt($("#priceMaxInput").val());
            // 验证数据
            if (isNaN(priceMin)) priceMin = 0;
            if (isNaN(priceMax)) priceMax = 999999;
            if (
                !(typeof priceMin == "number" && priceMin >= 0) ||
                !(typeof priceMax == "number" && priceMax >= 0) ||
                !(priceMin <= priceMax)
            ) {
                alert("非法数据, 请检查数据合理性");
                return;
            } else {
                // 检验有效, 赋值
                min = priceMin;
                max = priceMax;
                loadStart = 0;
                loadedCard = 0;
                loadCard();
            }
        })
    }

    function addListenerScroll() {
        $(window).scroll(function () {
            // 滚轮监听

            var scrollPosition = $(this).scrollTop();
            var itemNavHeight = $("#itemNav").height();
            // 根据两个位置大小关系, 确定导航栏的显示方式

            // 如果itemNav没有置顶
            if (!$("#itemNav").hasClass("fixed-top")) {
                var itemNavHeight = $("#itemNav").height();
                var itemNavOffset = $("#itemNav").offset().top;
                if (scrollPosition >= itemNavOffset - 25) {
                    $("#topNav").css("display", "none");
                    $("#itemNav").addClass("fixed-top");
                    $("#itemNavPositioner").height(itemNavHeight);
                }
            } else {
                // 如果itemNav置顶
                var itemNavHeight = $("#itemNavPositioner").height();
                var itemNavOffset = $("#itemNavPositioner").offset().top;
                if (scrollPosition <= itemNavOffset - 25) {
                    $("#topNav").css("display", "");
                    $("#itemNav").removeClass("fixed-top");
                    $("#itemNavPositioner").height('0');
                }
            }

            // 动态加载
            var documentHeight = $(document).height();
            var windowHeight = $(window).innerHeight();
            if (scrollPosition + windowHeight * 1.6 >= documentHeight) {
                loadCard();
            }

        });
    }

    function addListenerSearch() {
        // 搜索事件
        $("#topSearch").keydown(function () {
            if (event.which == 13) {
                search = $("#topSearch").val();
                if (search == "") search = "null";
                loadedCard = 0;
                loadStart = 0;
                loadCard();
            }
        });
    }

    function addListenerShowOffShelf() {
        // 显示下架开关点击事件
        $("#showItemOffShelf").click(function () {
            if (!$("#showItemOffShelf").hasClass("active")) {
                showOffShelf = true
                $("#showItemOffShelfIcon").removeClass("fa-toggle-off")
                $("#showItemOffShelfIcon").addClass("fa-toggle-on")
                $("#showItemOffShelf").addClass("active")
                // $(".offShelf").css("width", "33%");
                exeMasonry()
            } else {
                showOffShelf = false
                $("#showItemOffShelfIcon").removeClass("fa-toggle-on")
                $("#showItemOffShelfIcon").addClass("fa-toggle-off")
                $("#showItemOffShelf").removeClass("active")
                // $(".offShelf").css("width", "0");
                exeMasonry()
            }
        })
    }

    function addListeners(){
        addListenerSetRangeButton()
        addListenerScroll()
        addListenerSearch()
        addListenerShowOffShelf()
    }
</script>